﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;

namespace SharpSoft.WPF.Controls
{
    /// <summary>
    /// 为控件提供相对位置|尺寸的支持(注意，设置相对位置前，控件必须先加入一个容器，一般为<see cref="Canvas"/>)
    /// </summary>
    public static class RelativePosition
    {

        public static Point GetPosition(DependencyObject obj)
        {
            return (Point)obj.GetValue(PositionProperty);
        }

        public static void SetPosition(DependencyObject obj, Point value)
        {
            obj.SetValue(PositionProperty, value);
            PositionChanged(obj, new DependencyPropertyChangedEventArgs());
        }

        public static readonly DependencyProperty PositionProperty =
            DependencyProperty.RegisterAttached("Position", typeof(Point), typeof(FrameworkElement), new PropertyMetadata(new Point(0, 0), new PropertyChangedCallback(PositionChanged)));

        private static void PositionChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            if (sender is FrameworkElement ui)
            { 
                if (ui.Parent is Canvas canvas)
                {
                    var p = GetPosition(ui);
                    Canvas.SetLeft(ui, canvas.ActualWidth * p.X);
                    Canvas.SetTop(ui, canvas.ActualHeight * p.Y);
                    canvas.SizeChanged += (ss, ee) =>
                    {
                        var p1 = GetPosition(ui);
                        Canvas.SetLeft(ui, canvas.ActualWidth * p1.X);
                        Canvas.SetTop(ui, canvas.ActualHeight * p1.Y);
                    }; 
                }
            }
        }
         

        private static void SizeChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
             
            if (sender is FrameworkElement ui)
            {
                if (ui.Parent is FrameworkElement parent)
                {
                    var s = GetSize(ui);
                    ui.Width = parent.ActualWidth * s.Width;
                    ui.Height = parent.ActualHeight * s.Height;
                    parent.SizeChanged += (ss, ee) =>
                    {
                        var s1 = GetSize(ui);
                        ui.Width = parent.ActualWidth * s1.Width;
                        ui.Height = parent.ActualHeight * s1.Height;
                    };
                }
            }
        }

        public static Size GetSize(DependencyObject obj)
        {
            return (Size)obj.GetValue(SizeProperty);
        }

        public static void SetSize(DependencyObject obj, Size value)
        {
            obj.SetValue(SizeProperty, value);
            SizeChanged(obj,new DependencyPropertyChangedEventArgs());
        }

        public static readonly DependencyProperty SizeProperty =
            DependencyProperty.RegisterAttached("Size", typeof(Size), typeof(FrameworkElement), new PropertyMetadata(new Size(1, 1), new PropertyChangedCallback(SizeChanged)));



    }
}
